import numpy as np
import cv2 as cv
import glob
import os
from PIL import Image

rootimg = "/media/wz209/a29353b7-1090-433f-b452-b4ce827adb17/JZT/segmentation/Derm7pt/k_folder/img/f4"

save_root = '/media/wz209/a29353b7-1090-433f-b452-b4ce827adb17/JZT/segmentation/Derm7pt/k_folder/all'
fold = sorted(os.listdir(rootimg))

for i, p in enumerate(fold):
	
	pall = p
	print(i, p)
	p = rootimg + '/' + p
	
	img = Image.open(p)
	img = cv.cvtColor(np.array(img), cv.COLOR_RGB2BGR)
	img = cv.resize(img, (256, 256))
	
	mask = Image.open(p.replace('img', 'mask').replace('.jpg', '_segmentation.png'))
	mask = mask.resize((256, 256), Image.NEAREST)
	mask = np.array(mask)
	
	
	premask = Image.open(p.replace('img', 'mask').replace('.jpg', '_predict.png'))
	premask = np.array(premask)
	
	contour_mask, _ = cv.findContours(mask, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
	contour_premask, _ = cv.findContours(premask, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
	
	
	# print(contour_mask)
	
	img = cv.drawContours(img, contour_mask, -1, (255, 0, 0), 1)  # 蓝色
	img2 = cv.drawContours(img, contour_premask, -1, (0, 0, 255), 1)  # 红色
	
	path = os.path.join(save_root, pall.split('.')[0] + '_all.jpg')
	print(path)
	cv.imwrite(path, img2)
	
	# cv.imshow("123", img2)
	# cv.waitKey(0)
